package com.xiaotu.impRunnable;

/**
 * @Author xiaotu
 * @Description //TODO
 * @Date 2022/3/24 17:10
 * @ClassName mythread04
 * @Version 1.0
 * 多个线程操作同一个对象
 **/

public class mythread04 implements Runnable {
    private Integer tickeNums = 20;

    @Override
    public void run() {
        sellTicke();
    }

    public void sellTicke() {
            while (true) {
                synchronized (mythread04.class) {
                if (tickeNums <= 0) {
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "拿到了第" + tickeNums-- + "张票");
            }
        }
    }

    public static void main(String[] args) {
        mythread04 mythread04 = new mythread04();
        new Thread(mythread04, "小虎").start();
        new Thread(mythread04, "老师").start();
        new Thread(mythread04, "黄牛").start();
    }
}
